﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AssignDataFromTC.CApAssignDataFromTCCmdEnabler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using Tecnomatix.Engineering;
using UiInternalControls;


namespace DnProcessSimulateCommands.AssignDataFromTC;

internal class CApAssignDataFromTCCmdEnabler : ITxCommandEnabler
{
  private bool _isDataLoaded;

  public CApAssignDataFromTCCmdEnabler()
  {
    this.RegisterToEvents();
    this.setIsDataLoaded();
  }

  public bool Enable => this._isDataLoaded && !TxTcStudyReservation.IsReadOnly();

  private void OnDocumentLoaded(object sender, TxDocumentCollection_DocumentLoadedEventArgs args)
  {
    this.setIsDataLoaded();
  }

  private void OnActiveDocumentUnloading(object sender, TxDocument_UnloadingEventArgs args)
  {
    this._isDataLoaded = false;
  }

  internal void RegisterToEvents()
  {
    TxApplication.DocumentCollection.DocumentLoaded += new TxDocumentCollection_DocumentLoadedEventHandler(this.OnDocumentLoaded);
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.OnActiveDocumentUnloading);
  }

  internal void UnregisterFromEvents()
  {
    TxApplication.DocumentCollection.DocumentLoaded -= new TxDocumentCollection_DocumentLoadedEventHandler(this.OnDocumentLoaded);
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.OnActiveDocumentUnloading);
  }

  internal void setIsDataLoaded()
  {
    ITxPlatformGlobalServicesProvider servicesProvider = TxApplication.ActiveDocument.PlatformGlobalServicesProvider;
    this._isDataLoaded = servicesProvider != null && servicesProvider.IsDataLoaded();
  }
}
